﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SSS.RFIDWrapper;
using SSS.DB;
using SSS.Classes;

namespace SSS.Detect
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        RFIDReadWrapper reader;
        Boolean stopper = false;
        string LeerText = "";

        public MainWindow()
        {
            InitializeComponent();

            //int maxWidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
            //int maxHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;

            //var ratioX = (double)maxWidth / img_background.Width;
            //var ratioY = (double)maxHeight / img_background.Height;
            //var ratio = Math.Min(ratioX, ratioY);

            //img_background.Width = (int)(img_background.Width * ratio);
            //img_background.Height = (int)(img_background.Height * ratio);

            //lb_output.Height = lb_output.Height * ratio;
            //lb_output.Width = lb_output.Width * ratio;
            //lb_output.FontSize = lb_output.FontSize * ratio;

            lb_output.Content = LeerText;
            reader = new RFIDReadWrapper();
        }

        private void SleepThread(long ticks)
        {
            // Note: a tick is 100 nanoseconds
            long dtEnd = DateTime.Now.AddTicks(ticks).Ticks;

            while (DateTime.Now.Ticks < dtEnd)
            {
                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, (System.Windows.Threading.DispatcherOperationCallback)delegate(object unused) { return null; }, null);
            }
        }


        private void StartDetect()
        {
            DBAccess DB = new DBAccess();
            int KundenID;
            string LeserID = reader.GetReaderID();
            string ProductID;

            if (LeserID == "")
            {
                MessageBox.Show("Keine ReaderID lesbar");
                reader.StopReader();
                return;
            }


            lb_output.Content = LeerText;

            while (!stopper)
            {
                try
                {
                    KundenID = Int32.Parse(reader.GetCustomerID());

                    ProductID = DB.getProductIDfromReader(LeserID);
                    lb_output.Content = DB.getSize(KundenID, ProductID);

                    //lb_output.Invalidate();
                    //lb_output.Update();
                    //lb_output.Refresh();
                    //Application.DoEvents();

                    if (!stopper)
                        SleepThread(50000000);
                    lb_output.Content = LeerText;
                }
                catch (Exception)
                {
                    continue;
                    // throw;
                }
            }
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var window = (Window)sender;
            window.WindowStyle = WindowStyle.SingleBorderWindow;
            window.Topmost = false;
            window.WindowState = WindowState.Normal;

            stopper = true;
            reader.StopReader();
        }

        private void Window_ContentRendered(object sender, EventArgs e)
        {
            StartDetect();
        }

        private void Window_Initialized(object sender, EventArgs e)
        {
            var window = (Window)sender;
            window.WindowStyle = WindowStyle.None;
            window.Topmost = true;
            window.WindowState = WindowState.Maximized;
        }

        private void image1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Close();
        }
    }
}
